<?php
namespace api\modules\v1\controllers\goods;

use api\controllers\OnAuthController;

class TaobaoController extends OnAuthController
{
    public $modelClass = 'common\models\goods\GoodsCommon';

    protected $client = null;
    protected $app_key = '28181614';
    protected $app_secret = '8798f218201fe8bd41a542064f617686';
    protected $adzone_id = '109811750212';

    protected $page_size = '20';

    public $optional = ['optimus-material','content-get','activitylink','uatm-favorites','uatm-favorites-item','activity'];

    private function client(){
        if($this->client == null){
            $c = new \TopClient;
            $c->appkey = $this->app_key;
            $c->secretKey = $this->app_secret;
            $this->client = $c;
            return $c;
        }else return $this->client;
    }

    /**
     * 选品库列表
     * @return mixed|\ResultSet|\SimpleXMLElement
     */
    public function actionUatmFavorites(){
        $request = \Yii::$app->request;
        $page = $request->get('page',1);

        $req = new \TbkUatmFavoritesGetRequest;
        $req->setPageNo($page);
        $req->setPageSize($this->page_size);
        $req->setFields("favorites_title,favorites_id,type");
        $req->setType("1");
        $resp = $this->client()->execute($req);
        return $resp;
    }

    /**
     * 选品库商品
     * @return mixed|\ResultSet|\SimpleXMLElement
     */
    public function actionUatmFavoritesItem(){
        $request = \Yii::$app->request;
        $page = $request->get('page',1);
        $favorites_id = $request->get('favorites_id',0);

        $req = new \TbkUatmFavoritesItemGetRequest;
        $req->setPlatform("1");
        $req->setPageSize($this->page_size);
        $req->setAdzoneId($this->adzone_id);
        $req->setUnid("3456");
        $req->setFavoritesId($favorites_id);
        $req->setPageNo($page);
        $req->setFields("num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,status,type,coupon_info,commission_rate");
        $resp = $this->client()->execute($req);
        return $resp;
    }

    public function actionActivity(){
        $data = [
            [
                'type'=>'image',
                'url'=>'https://gw.alicdn.com/tfs/TB1o6akxsbpK1RjSZFyXXX_qFXa-480-180.jpg',
                'link'=>'https://s.click.taobao.com/t?e=m%3D2%26s%3DfV6%2BizoHfOYcQipKwQzePCperVdZeJviK7Vc7tFgwiFRAdhuF14FMUhe8gQFtbcy79%2FTFaMDK6SEHclso3ximodVzzXowBKqYRm6QgRadh9wU7FcxqoN3t0uB1f4fThH4gqXglJHiXPLQPEJLPAWAV3WV7X8X8sdfIrCKoPZYrQGZ%2FstJHrpqDhoiH7Ccg41JPwiig1bxLM7kJwmAcM4EgsysiBm1rjRklzFeKMz7Cdh5akHA5xEOINGWG%2FIQnGlrNjyQlIrIlU%3D&union_lens=lensId%3APUB%401575022729%400bb36611_0d42_16eb6ab99f5_05de%4001'
            ],
            [
                'type'=>'image',
                'url'=>'https://img.alicdn.com/tfs/TB15C8QeAL0gK0jSZFAXXcA9pXa-440-180.png',
                'link'=>'https://s.click.taobao.com/t?e=m%3D2%26s%3DFyy8m1JuOAkcQipKwQzePCperVdZeJviK7Vc7tFgwiFRAdhuF14FMabLY5Fba8DOxq3IhSJN6GSEHclso3ximodVzzXowBKqYRm6QgRadh9wU7FcxqoN3t0uB1f4fThH4gqXglJHiXPLQPEJLPAWAV3WV7X8X8sdfIrCKoPZYrQGZ%2FstJHrpqDhoiH7Ccg41JPwiig1bxLM7kJwmAcM4EoXPJiNrCirAcPyVtAN7GcUAecwElPsmGrt%2FyZJbCYSL7jEs6g580ex7kLD8NfoX9ymYlubupY1t%2Bw0JUN3MbIKwKNwXS2f0JsYOae24fhW0&union_lens=lensId%3APUB%401575022764%400b1b8c89_0d68_16eb6ac214a_05ea%4001'
            ]
        ];
        return $data;
    }
    /**
     * 获取活动链接
     * @return mixed|\ResultSet|\SimpleXMLElement
     */
    public function actionActivitylink(){
        $req = new \TbkActivitylinkGetRequest;
        $req->setPlatform("2");
        $req->setUnionId("demo");
        $req->setAdzoneId($this->adzone_id);
        $req->setPromotionSceneId("1574664919372");
//        $req->setSubPid("mm_123_123_123");
//        $req->setRelationId("23");
        $resp = $this->client()->execute($req);
        return $resp;
    }

    /**
     * 图文内容
     * @return mixed|\ResultSet|\SimpleXMLElement
     */
    public function actionContentGet(){
        $req = new \TbkContentGetRequest;
        $req->setAdzoneId($this->adzone_id);
        $req->setType("1");
//        $req->setBeforeTimestamp(time()*1000);
//        $req->setCount("10");
//        $req->setCid("2");
//        $req->setImageWidth("300");
//        $req->setImageHeight("300");
//        $req->setContentSet("1");
        $resp = $this->client()->execute($req);
        return $resp;
    }

    /**
     * 淘宝客-推广者-物料精选
     * @return mixed
     */
    public function actionOptimusMaterial(){
        $request = \Yii::$app->request;
        $page = $request->get('page',1);

        $material_id = 13366;//分类

        $req = new \TbkDgOptimusMaterialRequest;
        $req->setPageSize($this->page_size);
        $req->setAdzoneId($this->adzone_id);
        $req->setPageNo($page);
        $req->setMaterialId($material_id);
//        $req->setDeviceValue("xxx");
//        $req->setDeviceEncrypt("MD5");
//        $req->setDeviceType("IMEI");
//        $req->setContentId("323");
//        $req->setContentSource("xxx");
//        $req->setItemId("33243");
        $resp = $this->client()->execute($req);

        return $resp;
    }
}